//
//  HWHomeNormalProtocol.swift
//  HWanGame
//
//  Created by 王昌健 on 2023/11/16.
//  Copyright © 2023 94Wan. All rights reserved.
//

import UIKit

class HWHomeNormalProtocol: NSObject {
    var type = ""
    var model:HWHomeNormalModel?
    var clickTagView:((_ typeId:String,_ clickIndex:Int)->())?
    var didClickGame:((_ gameId:String)->())?
}

extension HWHomeNormalProtocol:UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 1 {
            didClickGame?(model?.topGames?.games?[indexPath.row].id ?? "")
        } else if indexPath.section == 2 {
            
        } else if indexPath.section == 3 {
            didClickGame?(model?.allGames?.games?[indexPath.row].id ?? "")
        }
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if type == "2" {
            if section == 0 || section == 1 {
                return 0
            } else {
                return 45*widthScale
            }
        } else {
            if section == 0 || section == 1 || section == 2 {
                return 0
            } else {
                return 45*widthScale
            }
        }
 
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = HWHomeTableHeader.headerOrFooterWithTableView(tableView: tableView)
        header.detailLabel.isHidden = true
        header.moreArrow.isHidden = true
        header.moreLabel.isHidden = true
        header.moreButton.isHidden = true
        if section == 2 {
            header.title = model?.huobao?.title ?? ""
        } else if section == 3 {
            header.title = model?.allGames?.title ?? ""
        }
        
        return header
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if section == 1 {
            return model?.topGames?.games?.count ?? 0
        } else if section == 3 {
            return model?.allGames?.games?.count ?? 0
        }
        
        if type == "2" {
            if section == 0 {
                if (model?.game_types.count ?? 0) > 0 {
                    return 1
                }
            } else if section == 2 {
                if (model?.huobao?.games?.count ?? 0) > 0 {
                    return 1
                }
            }
        } else if type == "5" {
            if section == 0 {
                if (model?.game_types.count ?? 0) > 0 {
                    return 1
                }
            }
        }
        return 0
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 4
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        /// 分类
        let typeCell = HWHomeNormalMoreTypeCell.cellWithTableView(tableView: tableView)
        /// 大图游戏
        let topGameCell = HWHomeNormalTopGameCell.cellWithTableView(tableView: tableView)
        /// 火爆推荐
        let fieryCell = HWHomeNormalFieryCell.cellWithTableView(tableView: tableView)
        /// 全部游戏
        let gameCell = HWHomeGameNormalCell.cellWithTableView(tableView: tableView)

        if indexPath.section == 0 {
            typeCell.model = self.model
            typeCell.clickTagView = {
                self.clickTagView?($0,$1)
            }
            return typeCell
        } else if indexPath.section == 1 {
            topGameCell.model = self.model?.topGames?.games?[indexPath.row]
            return topGameCell
        } else if indexPath.section == 2 {
            fieryCell.model = self.model?.huobao
            fieryCell.didClickFieryItem = {
                self.didClickGame?($0.id)
            }
            return fieryCell
        } else {
            gameCell.model = self.model?.allGames?.games?[indexPath.row]
            return gameCell
        }
    }
}
